<?php
$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('MessageHelper');
$MessageHelper = new MessageHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if(!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

switch ($this->Param['option']){
    case 'list':
        $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
        $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
        $where = array(
            '`m_uid` = ?' => $this->Param['uid']
        );
        $data = $MessageHelper->messageList($where, $page, $rp, $this->Param);
        $noread = $MessageHelper->messageCount(array('`m_status` = ?' => 0, '`m_uid` = ?' => $this->Param['uid']));
        $json_data = array('total' => $data['count'], 'list' => array(), 'page' => $page, 'pagetotal' => $data['pagetotal'], 'noread' => $noread);
        if (!empty($data['all'])){
            foreach ($data['all'] AS $rowNum => $row) {
                $order_row = $OrderHelper->orderRow($row['m_oid']);
                $row['status'] = $row['m_info']['status'];
                $row['o_number'] = $order_row['o_number'];
                $row['o_vip_name'] = $order_row['o_vip_name'];
                $json_data['list'][] = $row;
            }
        }
        $ApiHelper->logSave('messageindex', $this->Param);
        ErrorMsg::ApiMsg('请求成功', 0, $json_data);
        break;
    case 'read':
        $m_id = empty($this->Param['id']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['id'];
        $do = $MessageHelper->messageSave(array('m_status' => 1), array('`m_status` = ?' => 0, '`m_uid` = ?' => $this->Param['uid'], '`m_id` = ?' => $m_id));
        if($do){
            ErrorMsg::ApiMsg('处理成功', 0);
        }else{
            ErrorMsg::ApiMsg('处理失败');
        }
        break;
    default :
}